﻿<body style="padding: 2px; height: 100%; text-align: center;">
    <div id="layout">
        <ipnut type="hidden" id="MenuNo" value="Role" />
        <div position="center" title="角色管理">
            <form id="mainform">
                <div id="maingrid" style="margin: 2px;">
                </div>
            </form>
        </div>
    </div>
    <ul class="iconlist">
    </ul>
    <script type="text/javascript">
        var selectCombox;
        var selectController;
        //验证
        var maingform = $("#mainform");
        $.metadata.setType("attr", "validate");
        iTools.validate(maingform, { debug: true });
        //覆盖本页面grid的loading效果
        iTools.overrideGridLoading();

        function toolbarBtnItemClick(item) {
            var editingrow = grid.getEditingRow();
            switch (item.id) {
                case "Add":
                    if (editingrow == null) {
                        addNewRow();
                    } else {
                        iTools.tip('请先提交或取消修改');
                    }
                    break;
                case "Update":
                    if (editingrow == null) {
                        beginEdit();
                    } else {
                        iTools.tip('请先提交或取消修改');
                    }
                    break;
                case "Save":
                    if (editingrow != null) {
                        grid.endEdit(editingrow);
                    } else {
                        iTools.tip('现在不在编辑状态!');
                    }
                    break;
                case "Cancel":
                    if (editingrow != null) {
                        grid.cancelEdit(editingrow);
                    } else {
                        iTools.tip('现在不在编辑状态!');
                    }
                    break;
                case "Delete":
                    $.ligerDialog.confirm('确定删除吗(该删除操作只是软删除)?', function (confirm) {
                        if (confirm)
                            f_delete();
                    });
                    break;
                case "RealDelete":
                    $.ligerDialog.confirm('确定删除吗(该删除操作将会永久删除,一旦删除将无法挽回)?', function (confirm) {
                        if (confirm) {
                            //                            iTools.showError('不允许永久删除,未开放此功能!');
                            f_realdelete();
                        }
                    });
                    break;
            }
        }
        function f_reload() {
            grid.loadData();
        }
        function f_realdelete() {
            var selected = grid.getSelected();
            if (selected) {
                if (!selected.RID) {
                    grid.deleteRow(selected);
                    return;
                }
                var data = {};
                data.RID = selected.RID;
                $.ajax({
                    url: '/Role/RealDelete',
                    loading: '正在删除中...',
                    data: data,
                    success: function (data) {
                        if (data == "CanNotDelete")
                            iTools.showError('不能针对超级管理员角色进行删除操作');
                        else
                            iTools.showSuccess('删除成功');
                        f_reload();
                    },
                    error: function (message) {
                        iTools.showError(message);
                    }
                });
            }
            else {
                iTools.tip('请选择行!');
            }
        }
        function f_delete() {
            var selected = grid.getSelected();
            if (selected) {
                if (!selected.RID) {
                    grid.deleteRow(selected);
                    return;
                }
                var data = selected;
                data.RID = selected.RID;
                $.ajax({
                    url: '/Role/Delete',
                    loading: '正在删除中...',
                    data: data,
                    success: function (data) {
                        if (data == "CanNotDelete")
                            iTools.showError('不能针对超级管理员角色进行删除操作');
                        else
                            iTools.showSuccess('删除成功');
                        f_reload();
                    },
                    error: function (message) {
                        iTools.showError(message);
                    }
                });
            }
            else {
                iTools.tip('请选择行!');
            }
        }


        var layout = $("#layout").ligerLayout();
        var grid = $("#maingrid").ligerGrid({
            columns: [
                  { display: 'RID', name: 'RID', hide: true },
                  {
                      display: '角色名称', name: 'RoleName', align: 'left', width: 100, minWidth: 60
                  , validate: { required: true }
                   , editor: { type: 'text' }
                  },
                   {
                       display: '当前状态', name: 'IsVisible', isSort: false, align: 'left', width: 100, minWidth: 60, validate: { required: true }
                  , editor: { type: 'checkbox' }
                  , render: function (item) {
                      if (item.IsVisible)
                          return "启用";
                      else
                          return "禁用";
                  }
                   },
              {
                  display: '描述', name: 'Description', isSort: false, align: 'left', width: 300, minWidth: 60
                , editor: { type: 'text' }
              }
            ], dataAction: 'server', pageSize: 10, toolbar: {}, sortName: 'RID', url: '/Role/GetAllRolesForGrid',
            width: '98%', height: '100%', heightDiff: -5, checkbox: false, usePager: true, enabledEdit: true, clickToEdit: false,
            fixedCellHeight: true, rowHeight: 25
        });

        //本地按钮
        var toolbarOptions = [
               { text: '保存', id: 'Save', click: toolbarBtnItemClick, img: "@Url.Content("~/Content/icons/silkicons/page_save.png")" },
                  { text: '取消', id: 'Cancel', click: toolbarBtnItemClick, img: "@Url.Content("~/Content/icons/silkicons/cancel.png")" }
        ];
        //加载toolbar
        iTools.loadToolbar(grid, toolbarBtnItemClick, toolbarOptions);

        grid.bind('beforeSubmitEdit', function (e) {
            if (!iTools.validator.form()) {
                iTools.showInvalid();
                return false;
            }
            return true;
        });
        grid.bind('afterSubmitEdit', function (e) {
            var isAddNew = e.record['__status'] == "add";
            var data = e.newdata;
            if (!isAddNew) {
                data.RoleID = e.record.RoleID;
            }
            $.ajax({
                loading: '正在保存数据中...',
                url: '/Role/' + (isAddNew ? "Add" : "Update"),
                data: data,
                success: function () {
                    grid.loadData();
                    iTools.tip('保存成功!');
                },
                error: function (message) {
                    iTools.tip(message);
                }
            });
        });
        function beginEdit() {
            var row = grid.getSelectedRow();
            if (!row) { iTools.tip('请选择行'); return; }
            grid.beginEdit(row);
        }
        function addNewRow() {
            grid.addEditRow();
        }


    </script>
</body>
